package com.ctrl.riveradministrator.mvp.view.activity.news;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.mvp.model.News;

public class NewsDetailActivity extends BaseActivity {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private TextView title, time;
    private WebView webView;
    private News news;

    @Override
    protected void initParams(Bundle parms) {
        news = (News) getIntent().getSerializableExtra("news");
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_news_detail;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) $(R.id.toolbar);
        titleTv = (TextView) $(R.id.title_tv);
        title = (TextView) $(R.id.title);
        time = (TextView) $(R.id.time);
        leftMenu = (ImageView) $(R.id.left_iv);
        webView = (WebView) $(R.id.web);
        webView.getSettings().setBuiltInZoomControls(true); //显示放大缩小 controler
        webView.getSettings().setSupportZoom(true); //可以缩放
        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(news.getNewsTitle());
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        title.setText(news.getNewsTitle());
        time.setText(news.getCreateTime());
        Log.d("sss", "::" + news.getNewsContent());
        String content  = news.getNewsContent();
        content = content.replace("<img src","<img style='width:100%' src");
        webView.loadDataWithBaseURL(null, "<html>"
                + "<head>"
                + "<title></title>"
                + "</head>"
                + "<body>"
                + content
                + "</body>"
                + "</html>", "text/html", "utf-8", null);
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
